package com.huoxingy.print;

import com.huoxingy.print.domain.SalesFact;
import com.huoxingy.print.domain.SalesItemFact;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hanxi on 2018/10/8.
 */
@Data
@Component
@Slf4j
public class SalesFactJsonParser {
    private SalesFact salesFact;
    private List<SalesItemFact> items;
    private Configuration jsonConfig;

    @Autowired
    public SalesFactJsonParser(Configuration jsonConfig) {
        this.jsonConfig = jsonConfig;
    }

    public void parse(String json) {
        DocumentContext doc = JsonPath.using(jsonConfig).parse(json);

        String status = doc.read("$.status");
        if (status.equals("FINISHED") || status.equals("UNFINISHED")) {
            this.salesFact = new SalesFact();
            salesFact.setId(doc.read("$.id"));
            salesFact.setAmount(doc.read("$.amount", Double.class));
            salesFact.setCommission(doc.read("$.commission", Double.class));
            salesFact.setEmployeeUsername(doc.read("$.creatorId"));
            salesFact.setMemberId(doc.read("$.memberId"));
            salesFact.setPaymentChannel(doc.read("$.channel"));
            salesFact.setQty(doc.read("$.count", Short.class));
            salesFact.setSource(doc.read("$.source"));
            salesFact.setStoreId(doc.read("$.storeId"));
            salesFact.setCode(doc.read("$.code"));

            long paymentTimeInMills = doc.read("$.paymentTime");
            salesFact.setPaymentTime(new Date(paymentTimeInMills));

            int itemLength = doc.read("$.items.length()");
            this.items = new ArrayList<>();
            for (int i = 0; i < itemLength; i++) {
                boolean enabled = doc.read(String.format("$.items[%1$d].enabled", i));
                if (!enabled) {
                    continue;
                }

                SalesItemFact item = new SalesItemFact();
                item.setPaymentTime(salesFact.getPaymentTime());
                item.setSource(salesFact.getSource());
                item.setStoreId(salesFact.getStoreId());
                item.setMemberId(salesFact.getMemberId());
                item.setEmployeeUsername(salesFact.getEmployeeUsername());

                String id = doc.read(String.format("$.items[%1$s].id", i));
                item.setId(id);

                item.setOrderCode(doc.read("$.code"));
                item.setSalesPrice(doc.read(String.format("$.items[%1$d].salesPrice", i), Double.class));
                item.setCommission(doc.read(String.format("$.items[%1$d].commission", i), Double.class));
                item.setProductId(doc.read(String.format("$.items[%1$d].productId", i)));
                item.setQty(doc.read(String.format("$.items[%1$d].quantity", i), Short.class));

                items.add(item);
            }
        } else {
            log.warn("This sales order is not paid, just ignore. The message payload is:{}", json);
        }


    }
}
